{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# 入门计算机视觉系列课程[3]-让机器学习起来"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 环境测试"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 79,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "import cv2\n",
    "import numpy as np"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 80,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "a=[1,2,3]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 81,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello friends，welcome 明明老师的课程 22:12 ......... ----------------------------- [1, 2, 3]\n"
     ]
    }
   ],
   "source": [
    "print(\"Hello friends，welcome 明明老师的课程 22:12 .........\",29*\"-\",\"%s\"%(a))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 模型学习好的样子"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 模型y = weights x feature  + bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 82,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "[0.6 0.4]\n",
      "[0.76 0.24]\n"
     ]
    }
   ],
   "source": [
    "# 生成图片1 \n",
    "image1 = np.array([[0,0,1,0,0],[0,1,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0]])\n",
    "# 生成图片0\n",
    "image0 = np.array([[0,0,1,1,0],[0,1,0,0,1],[0,1,0,0,1],[0,1,0,0,1],[0,0,1,1,0]])\n",
    "def get_feature(image):\n",
    "    hist=np.array([0,0])\n",
    "    for i in range(image.shape[0]):\n",
    "        for j in range(image.shape[1]):\n",
    "            hist[image[i,j]]+=1\n",
    "    hist= hist/image.size\n",
    "    print(hist)\n",
    "    return hist\n",
    "fea0= get_feature(image0)\n",
    "fea1= get_feature(image1)\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 84,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.66243831]\n",
      "模型对image1的判断结果y1=[1.68604633]\n",
      "当 weights 和 b 有一组合适的值，使y0=0,y1=1时，模型就是训练好了\n"
     ]
    }
   ],
   "source": [
    "weights=np.random.rand(1,2)\n",
    "b = np.random.rand(1,1)\n",
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "print(\"当 weights 和 b 有一组合适的值，使y0=0,y1=1时，模型就是训练好了\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 这个function实现了判断工程\n",
    "# 通常我们叫它model"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 你早就知道如何让机器进行学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 如何找到合适的weights 和 bias值，时y0=0,y1=1\n",
    "# 实际上就是解这样一个方程组\n",
    "# weights * fea0 + bias = 0  方程[1]\n",
    "# weights * fea1 + bias = 1  方程[2]\n",
    "# 这里weights 和 bias是未知量\n",
    "\n",
    "# 你看出来了：解方程组帮助模型找到了合适的weights值。\n",
    "# 你早就会解方程组了对不对？\n",
    "# 你早就知道如何让机器进行学习\n",
    "# 其实你早就知道如何针对简单的问题进行建模，然后学习一个简单的模型。\n"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 85,
   "metadata": {},
   "outputs": [
    {
     "data": {
      "image/png": "iVBORw0KGgoAAAANSUhEUgAAANAAAAD4CAYAAACdW2gvAAAAOXRFWHRTb2Z0d2FyZQBNYXRwbG90\nbGliIHZ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od7a9Mck527+SdEjSk0k+WOXxgzqHmlO6x2lop3QnuWqgVlugJOea/y5K\nOihp23RHA8apzTL3t9i+7dL3kv4g6UTXgwFj0GYv3K8lHWw2pzdI+muSv3U6FTASrT4DrflJ+QyE\nKbhuPgMBWBkBAQUEBBQQEFBAQEABAQEFBAQUEBBQQEBAAQEBBQQEFBAQUNDVGqnfSPrPFJ7nl81z\nlUzxoMSpzDNFzDPZtGb6zUp3dnI09rTYPjqkKwAxz+qGNo/U/Uy8hQMKCAgoGHpA+/oe4ArMs7qh\nzSN1PNOgPwMBQzf0LRAwaAQEFAwyINvztr+wfcb2swOYZ7/tRduDuJyX7U22D9s+Zfuk7b09z3OT\n7U9sf9bM83yf81xie8b2p7bf6eo1BheQ7RlJf5b0R0lbJO22vaXfqfSKpPmeZ1juoqSnk2yRdL+k\nP/X8/+i/knYk+Z2keyXN276/x3ku2SvpdJcvMLiAtHTV0zNJvkzyg6QDkh7pc6DmMsbf9jnDckm+\nTnKs+f68ln5INvY4T5JcaG5uaL563Ttle1bSg5Je6vJ1hhjQRklfLbt9Vj3+cAyd7c2S7pP0cc9z\nzNg+LmlR0qEkvc4j6UVJz0j6qcsXGWJAaMn2rZLelPRUku/7nCXJj0nulTQraZvt3/Y1i+2HJC0m\nWej6tYYY0DlJm5bdnm3uwzK2N2gpnteSvNX3PJck+U7SYfX7mXG7pIeblRUPSNph+9UuXmiIAR2R\ndLftu2zfKGmXpLd7nmlQvHR4+cuSTid5YQDz3GH79ub7myXtlPR5X/MkeS7JbJLNWvr5eS/Jo128\n1uACSnJR0hOS3tXSh+M3kpzscybbr0v6SNI9ts/afrzPebT0N+xjWvqb9Xjz9UCP89wp6bDtf2rp\nL8BDSTrbdTwkHMoDFAxuCwSMCQEBBQQEFBAQUEBAQAEBAQUEBBT8D3YfIcy8VXWQAAAAAElFTkSu\nQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    },
    {
     "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AgoICCj4H5IKQAfxgp15AAAAAElF\nTkSuQmCC\n",
      "text/plain": [
       "<Figure size 432x288 with 1 Axes>"
      ]
     },
     "metadata": {},
     "output_type": "display_data"
    }
   ],
   "source": [
    "# 生成图片1 \n",
    "image1 = np.array([[0,0,1,0,0],[0,1,1,0,0],[0,0,1,0,0],[0,0,1,0,0],[0,0,1,0,0]])\n",
    "# 生成图片0\n",
    "image0 = np.array([[0,0,1,1,0],[0,1,0,0,1],[0,1,0,0,1],[0,1,0,0,1],[0,0,1,1,0]])\n",
    "\n",
    "# 生成图片3\n",
    "image3 = np.array([[0,0,1,1,0],[0,1,0,0,1],[0,0,0,0,1],[0,0,1,1,0],[0,0,0,0,1],[0,1,1,1,0]])\n",
    "\n",
    "# 生成图片4\n",
    "image4 = np.array([[0,0,1,0,0],[0,1,1,0,0],[1,0,1,0,0],[1,1,1,1,1],[0,0,1,0,0],[0,0,1,0,0]])\n",
    "\n",
    "\n",
    "import matplotlib.pyplot as plt\n",
    "plt.imshow(image3,cmap=\"gray\")\n",
    "plt.show()\n",
    "plt.imshow(image4,cmap=\"gray\")\n",
    "plt.show()\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 机器学习的本质"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 你看出来了：解方程组帮助模型找到了合适的weights值。\n",
    "# 你早就会解方程组了对不对？\n",
    "# 你早就知道如何让机器进行学习\n",
    "# 其实你早就知道如何针对简单的问题进行建模，然后学习一个简单的模型。\n",
    "\n",
    "# 机器学习的本质就是找到能够支持模型完成判断的那一组参数值\n",
    "# 用何种办法找到这组参数值都行\n",
    "# 简单的问题可以用解方程组的方法\n",
    "# 复杂的问题可以用复杂一点的方法"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 把机器学习的过程转化为求最小值的过程"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 43,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 如果我们有4张图片，image0,image1,image2,image3\n",
    "# 如果我们用解方程组的方法，我们需要解4个方程组\n",
    "\n",
    "# 如果我们有N张图片，image0,image1,image2,,,,imageN\n",
    "# 如果我们用解方程组的方法，我们需要解N个方程组\n",
    "\n",
    "# 方程组数量太多了，无法解了，\n",
    "# 这时候我们就可以把解方程组这个过程，转化为求最小值这个过程\n",
    "\n",
    "# 对于image0 来说\n",
    "# weights*fea+bias 是模型的输出，为了描述方便，我们定义这个值为 y^\n",
    "# 0 是image0的类别，为了描述方便，我们定义这个值为 y*\n",
    "\n",
    "# 我们希望 y^ 等于 y* \n",
    "# 我们希望 abs(y^ - y*) =0\n",
    "# 我们希望 abs(y^ - y*) 尽量小\n",
    "# 我们希望 (y^ - y*)^2 尽量小\n",
    "\n",
    "\n",
    "# 所以，只要我们找到一组合适的weights 和 bias ,使(y^- y*)^2取得到最小值0,\n",
    "# 就相当于模型完成学习了\n",
    "\n",
    "# 所以我们用各种最优化方法来最小化(y^-y*)^2\n",
    "# 把y^ 和 y*本来的值代入进去：\n",
    "# (weights*fea + bias - 0)^2\n",
    "# fea 是已知的\n",
    "\n",
    "# 我们定义(y^-y*)^2为目标函数，loss函数\n",
    "\n",
    "\n"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 梯度下降算法实现机器的学习"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "# 1 初始化：给未知量初始值\n",
    "# 2 计算loss,loss=(y^-y*)^2\n",
    "# 3 计算loss对weights 和 bias的梯度 Dweights 和Dbias\n",
    "# 4 利用weights -= learning_rate*Dweghts,bias -= learning_rate* Dbias来更新weights和bias的值\n",
    "# 5 回到2 "
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 初始化"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 44,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.36478569]\n",
      "模型对image1的判断结果y1=[1.36871205]\n",
      "当 weights 和 b 有一组合适的值，使y0=0,y1=1时，模型就是训练好了\n"
     ]
    }
   ],
   "source": [
    "# 初始化\n",
    "weights=np.random.rand(1,2)\n",
    "b = np.random.rand(1,1)\n",
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "print(\"当 weights 和 b 有一组合适的值，使y0=0,y1=1时，模型就是训练好了\")"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 46,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.36478569]\n",
      "模型对image1的判断结果y1=[1.36871205]\n",
      "loss=[[3.73601268]]\n"
     ]
    }
   ],
   "source": [
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "loss = (y0-0)**2+ (y1-0)**2\n",
    "print(\"loss=%s\"%(loss))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": [
    "\n",
    "# Dweights = 2*(y0-0)*fea0 + 2*(y1-1)*fea1\n",
    "# Dbias = 2*(y0-1) + 2*(y1-1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 47,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dweights=[[2.19818515 1.26881034]]\n",
      "Dbias=[[1.4669955]]\n"
     ]
    }
   ],
   "source": [
    "Dweights = 2*(y0-0)*fea0 + 2*(y1-1)*fea1\n",
    "Dbias = 2*(y0-1) + 2*(y1-1)\n",
    "print(\"Dweights=%s\"%(Dweights))\n",
    "print(\"Dbias=%s\"%(Dbias))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 更新weights,bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 50,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weigths=[[0.87240478 0.85065317]]\n",
      "bias=[[0.49266826]]\n"
     ]
    }
   ],
   "source": [
    "weights -= Dweights * 0.001\n",
    "b -= Dbias * 0.001\n",
    "print(\"weigths=%s\"%(weights))\n",
    "print(\"bias=%s\"%(b))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 计算loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 58,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.35313204]\n",
      "模型对image1的判断结果y1=[1.35646561]\n",
      "loss=[[3.67096529]],loss在变小\n"
     ]
    }
   ],
   "source": [
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "loss = (y0-0)**2+ (y1-0)**2\n",
    "print(\"loss=%s,loss在变小\"%(loss))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 计算梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 60,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dweights=[[2.16558619 1.25360913]]\n",
      "Dbias=[[1.41919532]]\n"
     ]
    }
   ],
   "source": [
    "Dweights = 2*(y0-0)*fea0 + 2*(y1-1)*fea1\n",
    "Dbias = 2*(y0-1) + 2*(y1-1)\n",
    "print(\"Dweights=%s\"%(Dweights))\n",
    "print(\"Dbias=%s\"%(Dbias))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 更新weights,bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 62,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weigths=[[0.86806457 0.84814173]]\n",
      "bias=[[0.48981662]]\n"
     ]
    }
   ],
   "source": [
    "weights -= Dweights * 0.001\n",
    "b -= Dbias * 0.001\n",
    "print(\"weigths=%s\"%(weights))\n",
    "print(\"bias=%s\"%(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 计算loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 64,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.34991205]\n",
      "模型对image1的判断结果y1=[1.35309971]\n",
      "loss=[[3.65314137]],loss在变小\n"
     ]
    }
   ],
   "source": [
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "loss = (y0-0)**2+ (y1-0)**2\n",
    "print(\"loss=%s,loss在变小\"%(loss))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 计算梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 66,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dweights=[[2.15660602 1.2494175 ]]\n",
      "Dbias=[[1.40602352]]\n"
     ]
    }
   ],
   "source": [
    "Dweights = 2*(y0-0)*fea0 + 2*(y1-1)*fea1\n",
    "Dbias = 2*(y0-1) + 2*(y1-1)\n",
    "print(\"Dweights=%s\"%(Dweights))\n",
    "print(\"Dbias=%s\"%(Dbias))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 更新weights,bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 70,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weigths=[[0.86375136 0.84564289]]\n",
      "bias=[[0.48700457]]\n"
     ]
    }
   ],
   "source": [
    "weights -= Dweights * 0.001\n",
    "b -= Dbias * 0.001\n",
    "print(\"weigths=%s\"%(weights))\n",
    "print(\"bias=%s\"%(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 计算loss"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 72,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "模型对image0的判断结果y0=[1.34351254]\n",
      "模型对image1的判断结果y1=[1.3464099]\n",
      "loss=[[3.61784557]],loss在变小\n"
     ]
    }
   ],
   "source": [
    "y0 = weights@fea0+b\n",
    "print(\"模型对image0的判断结果y0=%s\"%(y0[0]))\n",
    "y1 = weights@fea1+b\n",
    "print(\"模型对image1的判断结果y1=%s\"%(y1[0]))\n",
    "loss = (y0-0)**2+ (y1-0)**2\n",
    "print(\"loss=%s,loss在变小\"%(loss))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 计算梯度"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 53,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Dweights=[[2.17462292 1.25782719]]\n",
      "Dbias=[[1.43245011]]\n"
     ]
    }
   ],
   "source": [
    "Dweights = 2*(y0-0)*fea0 + 2*(y1-1)*fea1\n",
    "Dbias = 2*(y0-1) + 2*(y1-1)\n",
    "print(\"Dweights=%s\"%(Dweights))\n",
    "print(\"Dbias=%s\"%(Dbias))"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {
    "collapsed": true
   },
   "source": [
    "### 更新weights,bias"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 54,
   "metadata": {
    "scrolled": true
   },
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "weigths=[[0.87023016 0.84939534]]\n",
      "bias=[[0.49123581]]\n"
     ]
    }
   ],
   "source": [
    "weights -= Dweights * 0.001\n",
    "b -= Dbias * 0.001\n",
    "print(\"weigths=%s\"%(weights))\n",
    "print(\"bias=%s\"%(b))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {
    "collapsed": true
   },
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python [conda env:python3.6.7]",
   "language": "python",
   "name": "conda-env-python3.6.7-py"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.6.7"
  },
  "toc": {
   "base_numbering": 1,
   "nav_menu": {},
   "number_sections": true,
   "sideBar": true,
   "skip_h1_title": false,
   "title_cell": "Table of Contents",
   "title_sidebar": "Contents",
   "toc_cell": false,
   "toc_position": {
    "height": "calc(100% - 180px)",
    "left": "10px",
    "top": "150px",
    "width": "200px"
   },
   "toc_section_display": true,
   "toc_window_display": true
  }
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
